﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKGeometryFuncs;
using YJKRevitStairs.StairsModel.CommonUnit;

namespace YJKRevitStairs.HYElevator.Kit
{
	internal class ModifyRoom
	{
		public ModifyRoom(Autodesk.Revit.DB.Document Doc, Autodesk.Revit.DB.View view, string strName)
		{
			this.m_Doc = Doc;
			this.m_view = view;
			this.m_strName = strName;
		}

		public void Run(Element elem)
		{
			XYZ max = elem.get_BoundingBox(this.m_view).Max;
			XYZ min = elem.get_BoundingBox(this.m_view).Min;
			XYZ xyz = YJKGeometryFuncs.Geometry.CalculatMidPoint(max, min);
			XYZ midPt = new XYZ(xyz.X, xyz.Y, this.m_view.GenLevel.Elevation);
			this.ModifyRoomByFamilyInstance(midPt);
		}

		private void ModifyRoomByFamilyInstance(XYZ MidPt)
		{
			RoomTagType roomTageType = this.GetRoomTageType("名称_无编号_面积_无单元");
			if (roomTageType != null)
			{
				roomTageType.Activate();
			}
			SubTransaction subTransaction = new SubTransaction(this.m_Doc);
			try
			{
				subTransaction.Start();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
				ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Rooms);
				filteredElementCollector.WherePasses(elementCategoryFilter);
				FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_Doc);
				ElementCategoryFilter elementCategoryFilter2 = new ElementCategoryFilter(BuiltInCategory.OST_RoomTags);
				List<Element> list = filteredElementCollector2.WherePasses(elementCategoryFilter2).WhereElementIsNotElementType().ToElements() as List<Element>;
				filteredElementCollector.WherePasses(elementCategoryFilter);
				bool flag = false;
				foreach (Element element in filteredElementCollector)
				{
					if (element is Room)
					{
						Room r = element as Room;
						flag = r.IsPointInRoom(MidPt);
						if (flag)
						{
							r.Name = this.m_strName;
							Element element2 = list.Find((Element e) => (e as RoomTag).Room.Id.Equals(r.Id));
							if (roomTageType != null && element2 != null)
							{
								(element2 as RoomTag).RoomTagType = roomTageType;
								break;
							}
							break;
						}
					}
				}
				if (!flag)
				{
					using (List<Room>.Enumerator enumerator2 = this.Create2ndGetRoomDataInfos().GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							Room r = enumerator2.Current;
							flag = r.IsPointInRoom(MidPt);
							if (flag)
							{
								r.Name = this.m_strName;
								FilteredElementCollector filteredElementCollector3 = new FilteredElementCollector(this.m_Doc);
								ElementCategoryFilter elementCategoryFilter3 = new ElementCategoryFilter(BuiltInCategory.OST_RoomTags);
								Element element3 = (filteredElementCollector3.WherePasses(elementCategoryFilter3).WhereElementIsNotElementType().ToElements() as List<Element>).Find((Element e) => (e as RoomTag).Room.Id.Equals(r.Id));
								if (roomTageType != null && element3 != null)
								{
									(element3 as RoomTag).RoomTagType = roomTageType;
								}
							}
							else
							{
								this.m_Doc.Delete(r.Id);
							}
						}
					}
				}
				subTransaction.Commit();
			}
			catch (Exception)
			{
				subTransaction.RollBack();
				throw;
			}
		}

		private List<Room> Create2ndGetRoomDataInfos()
		{
			List<Room> list = new List<Room>();
			double num = AssistFunc.mmToFeet(1000.0);
			num *= num;
			new SpatialElementBoundaryOptions();
			foreach (object obj in this.m_Doc.get_PlanTopology(this.m_view.GenLevel).Circuits)
			{
				PlanCircuit planCircuit = (PlanCircuit)obj;
				if (!planCircuit.IsRoomLocated && !Common.IsDoubleLessThanZero(planCircuit.Area - num))
				{
					Room item = this.m_Doc.Create.NewRoom(null, planCircuit);
					list.Add(item);
				}
			}
			return list;
		}

		private RoomTagType GetRoomTageType(string strTypeName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_RoomTags);
			filteredElementCollector.WherePasses(elementCategoryFilter).WhereElementIsElementType();
			foreach (Element element in filteredElementCollector)
			{
				RoomTagType roomTagType = element as RoomTagType;
				if (roomTagType.Name.Equals(strTypeName))
				{
					return roomTagType;
				}
			}
			return null;
		}

		private Document m_Doc;

		private Autodesk.Revit.DB.View m_view;

		private string m_strName;
	}
}
